<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class GSD_admin extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('admin_model');
    }

    public function user_view() {
        $data = $this->admin_model->menu();
        $data['users'] = $this->admin_model->get_all_user();
        $this->load->view('user_view.php', $data);
		
    }
 
   public function add_user() {

        $this->load->library('form_validation');
        $this->form_validation->set_rules('firstname', 'Firstname', 'required|xss_clean');
        $this->form_validation->set_rules('middlename', 'Middlename', 'required|xss_clean');
        $this->form_validation->set_rules('lastname', 'Lastname', 'required|xss_clean');

        //$this->form_validation->set_rules('userPic', 'Profile Picture', 'required|xss_clean');
        $this->form_validation->set_rules('username', 'Username', 'required|xss_clean');
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email|xss_clean');
        $this->form_validation->set_rules('password', 'Password', 'required|xss_clean');
        $this->form_validation->set_rules('passconf', 'Password confirmation', 'required|matches[password]|xss_clean');
        $this->form_validation->set_rules('roles', 'Role', 'required|xss_clean');
        if ($this->form_validation->run() == TRUE) {


            $config['upload_path'] = './ProfilePic/';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '100';
            $config['max_width'] = '1024';
            $config['max_height'] = '768';

            $this->load->library('upload', $config);

            if ($this->upload->do_upload('userPic')) {
                $upload_data = $this->upload->data();

                $file_name = $upload_data['file_name'];
                $users = array(
                    'fname' => $this->input->post('firstname'),
                    'mname' => $this->input->post('middlename'),
                    'lname' => $this->input->post('lastname'),
                    'uname' => $this->input->post('username'),
                    'email' => $this->input->post('email'),
                    'pword' => md5($this->input->post('password')),
                    'photo' => $file_name,
                    'club' => $this->input->post('club'),
                    'style' => $this->input->post('style'),
                    'role' => $this->input->post('roles'),
                    'bdate' => $this->input->post('bdate'),
                    'isactive' => $this->input->post(1),
                );
  
                $data = $this->admin_model->menu();
                

                $id = $this->admin_model->insert_user($users);

                /*                 * foreach ($this->input->post('myTable') as $ranks)
                  {
                  $user_rank = array(
                  'userID' => $id,
                  'rank' => $this->input->post('cell1'),
                  'examDate' => $this->input->post('cell2'),
                  'promotionType' => $this->input->post('cell3')

                  );
                  $this->admin_model->insert_rank($user_rank);

                  } */

                $this->view_profile($id);
                
            } else {
                $this->user_add();
            }
        }
    }

	
	 public function user_add() {
        $data = $this->admin_model->menu();
        $data['roles'] = $this->admin_model->get_role();
		$this->load->view('user_add.php',$data); 
    }
	
   

    function edit_user($user_id,$action) {

        $data = $this->admin_model->menu();
        $user = $this->admin_model->get_user($user_id);
		$data['roles'] = $this->admin_model->get_role();
        $this->load->library('form_validation');
        $this->form_validation->set_rules('firstname', 'Firstname', 'required|xss_clean');
        $this->form_validation->set_rules('middlename', 'Middlename', 'required|xss_clean');
        $this->form_validation->set_rules('lastname', 'Lastname', 'required|xss_clean');
        $this->form_validation->set_rules('username', 'Username', 'required|xss_clean');
        $this->form_validation->set_rules('email', 'Email', 'required|xss_clean');
        $this->form_validation->set_rules('password', 'Password', 'required|xss_clean');
        if (isset($_POST) && !empty($_POST)) {
            $data = array(
                'fname' => $this->input->post('firstname'),
                'mname' => $this->input->post('middlename'),
                'lname' => $this->input->post('lastname'),
                'uname' => $this->input->post('username'),
                'email' => $this->input->post('email'),
                'photo' => $this->input->post('image1'),
                'club' => $this->input->post('club'),
                'style' => $this->input->post('style'),
                'isactive' => $this->input->post(1),
            );
            $data['fname'] = $_POST['firstname'];
            if ($this->form_validation->run() == true) {
                $this->admin_model->update_user($user_id, $data);

                $this->view_profile($user_id);
            }
        }

        $data['user'] = $user;
	 
		 $this->load->view('user_edit', $data);
		 
    }
	
	 function view_profile($user_id) {
        $data = $this->admin_model->menu();
        $user = $this->admin_model->get_user($user_id);
        $data['user'] = $user;
        $this->load->view('userprofile', $data);
    }

    function logout() {
        $newdata = array(
            'uid' => '',
            'uname' => '',
            'password' => '',
            'logged_in' => FALSE,
        );
        $this->session->unset_userdata($newdata);
        $this->session->sess_destroy();
        $this->index();
    }
	
	public function history() 
    {
        $this->load->library('table');
		$this->load->helper('html');
		$this->load->model('admin_model');
		$data = $this->admin_model->menu();        
        $this->load->view("history.php",$data);
    }
	
	public function about_contact() 
    {
        $this->load->library('table');
		$this->load->helper('html');
		$this->load->model('admin_model');
		$data = $this->admin_model->menu();        
        $this->load->view("about_contact.php",$data);
    }


	public function user_email() 
    {
        $this->load->library('table');
		$this->load->helper('html');
		$this->load->model('admin_model');
		$data = $this->admin_model->menu();        
        $this->load->view("user_email.php",$data);
    }
	
	public function user_compose_email() 
    {
        $this->load->library('table');
		$this->load->helper('html');
		$this->load->model('admin_model');
		$data = $this->admin_model->menu();        
        $this->load->view("user_compose_mail.php",$data);
    }
	

     function club() {
        $this->load->library('table');
        $this->load->helper('html');
        $this->load->model('admin_model');
        $data = $this->admin_model->menu();
        $this->load->view("club_view.php", $data);
    }    
 
	public function view_clubprofile() 
    {
        $this->load->library('table');
		$this->load->helper('html');
		$this->load->model('admin_model');
		$data = $this->admin_model->menu();        
        $this->load->view("addminviewclubprofile.php",$data);
    }

	public function tournaments() 
    {
        $this->load->library('table');
		$this->load->helper('html');
		$this->load->model('admin_model');
		$data = $this->admin_model->menu();        
        $this->load->view("tournaments.php",$data);
		
	 }
	 
	  public function tournamentpage() 
    {
        $this->load->library('table');
		$this->load->helper('html');
		$this->load->model('admin_model');
		$data = $this->admin_model->menu();        
        $this->load->view("tournamentviewpage.php",$data);
    }
	
}

